<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- blkid | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- blkid</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在日常的运维工作中遇到过这样的情况，一台服务器上新装了两块磁盘，磁盘a（在服务器上显示为sdc）和磁盘b（在服务器上显示为sdd），有一次把这两块磁盘都拔掉了，然后再重新插上，重启机器，结果磁盘编号调换了，a变成了sdd，b变成了sdc（这是因为把磁盘插错了插槽），问题来了。通过上边的学习，你挂载磁盘是通过/dev/hdb1 这样的分区名字来挂载的，如果先前加入到了/etc/fstab 中，结果系统启动后则会挂载错分区。那么怎么样避免这样的情况发生？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这就用到了UUID，可以通过 blkid 命令获取各分区的UUID:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">/dev/sda1: UUID=<span class="string">"a593ff68-2db7-4371-8d8c-d936898e9ac9"</span> TYPE=<span class="string">"ext4"</span></div><div class="line">/dev/sda2: UUID=<span class="string">"ff042a91-b68f-4d64-9759-050c51dc9e8b"</span> TYPE=<span class="string">"swap"</span></div><div class="line">/dev/sda3: UUID=<span class="string">"95297b81-538d-4d96-870a-de90255b74f5"</span> TYPE=<span class="string">"ext4"</span></div><div class="line">/dev/sdb5: LABEL=<span class="string">"TEST"</span> UUID=<span class="string">"c61117ca-9176-4d0b-be4d-1b0f434359a7"</span> TYPE=<span class="string">"ext4"</span></div><div class="line">/dev/sdb6: UUID=<span class="string">"c271cb5a-cb46-42f4-9eb4-d2b1a5028e18"</span> SEC_TYPE=<span class="string">"ext2"</span> TYPE=<span class="string">"ext3"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样可以获得全部磁盘分区的UUID，如果格式化的时候指定了 LABEL 则该命令也会显示LABEL值，甚至连文件系统类型也会显示。当然这个命令后面也可以指定哪个分区：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># blkid /dev/sdb5</span></div><div class="line">/dev/sdb5: LABEL=<span class="string">"TEST"</span> UUID=<span class="string">"c61117ca-9176-4d0b-be4d-1b0f434359a7"</span> TYPE=<span class="string">"ext4"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;获得UUID后，如何使用它呢？</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># umount /newdir</span></div><div class="line">[root@localhost ~]<span class="comment"># mount UUID="c61117ca-9176-4d0b-be4d-1b0f434359a7" /newdir</span></div><div class="line">[root@localhost ~]<span class="comment"># df -h</span></div><div class="line">文件系统              容量  已用  可用 已用%% 挂载点</div><div class="line">/dev/sda3              14G  1.5G   12G  11% /</div><div class="line">tmpfs                 160M     0  160M   0% /dev/shm</div><div class="line">/dev/sda1              97M   27M   66M  29% /boot</div><div class="line">/dev/sdb5             989M   18M  921M   2% /newdir</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以把下面这行写到 /etc/fstab 中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">UUID=c61117ca-9176-4d0b-be4d-1b0f434359a7              /newdir                 ext4    defaults        0 0</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想让某个分区开机后就自动挂载，有两个办法可以实现：</p>
<ol>
<li>在 /etc/fstab 中添加一行，如上例中那行；</li>
<li>把挂载命令写到 /etc/rc.d/rc.local 文件中去，阿铭会经常把想要开机启动的命令加到这个文件中。系统启动完后会执行这个文件中的命令，所以只要你想开机后运行什么命令统统写入到这个文件下面吧，直接放到最后面即可，阿铭把挂载的命令放到该文件的最后一行了：</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /etc/rc.d/rc.local</span></div><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># This script will be executed *after* all the other init scripts.</span></div><div class="line"><span class="comment"># You can put your own initialization stuff in here if you don't</span></div><div class="line"><span class="comment"># want to do the full Sys V style init stuff.</span></div><div class="line"></div><div class="line">touch /var/lock/subsys/<span class="built_in">local</span></div><div class="line">mount UUID=<span class="string">"c61117ca-9176-4d0b-be4d-1b0f434359a7"</span> /newdir</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上两种方法，任选其一，介绍第二种方法其实也是一个小知识，如何让一些操作行为随系统启动而自动执行。另外一个小建议，那就是挂载磁盘分区的时候，尽量使用UUID或者LABEL这两种方法。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/76. Linux 命令- blkid/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
